package com.example.work2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ZhuYe extends AppCompatActivity {
    private String homeUrl = "https://www.wanandroid.com/article/list/0/json";
    private String logoutUrl = "https://www.wanandroid.com/user/logout/json";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        new Thread(){
            @Override
            public void run() {
                String s = getJsonString(homeUrl);
                System.out.println(s);
            }
        }.start();
    }

    public void onClick1(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    String logout = logout();
                    JSONObject object = new JSONObject(logout);
                    int errorCode = object.optInt("errorCode");
                    String errorMsg = object.optString("errorMsg");
                    if (errorCode==0){
                        System.out.println("退出成功");
                        Intent intent=new Intent();
                        intent.setClass(ZhuYe.this, MainActivity.class);
                        startActivity(intent);
                    }else {
                        System.out.println("退出失败");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private String logout(){
        try {
            URL url = new URL(logoutUrl);
            HttpURLConnection cn = (HttpURLConnection) url.openConnection();
            if (cn.getResponseCode()==200){
                InputStream inputStream = cn.getInputStream();
                String s = stream2String(inputStream);
                return s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private String getJsonString(String path){
        try {
            URL url=new URL(path);
            HttpURLConnection cn= (HttpURLConnection) url.openConnection();
            InputStream inputStream = cn.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer buffer = new StringBuffer();
            String line = null;
            while ((line=br.readLine())!=null){
                buffer.append(line);
            }
            return buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

//        try{
//            URL url=new URL(path);
//            HttpURLConnection cn= (HttpURLConnection) url.openConnection();
//            InputStream inputStream = cn.getInputStream();
//            byte[] bytes = new byte[1024];
//            int len = 0;
//            while ((len=inputStream.read(bytes))!=-1){
//                String s = new String(bytes);
//                return s;
//            }
//        } catch (Exception e){
//            e.printStackTrace();
//        }
//        return null;
    }

    private String stream2String(InputStream is) {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuffer sb = new StringBuffer();
        String line;
        try {
            while ((line=br.readLine()) != null) {
                sb.append(line);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return sb.toString();
    }
}